/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#pragma once

void Memory_Init (void);


class QALLOCATORBASE
{
public:
	virtual void *Alloc (int size) = 0;
	virtual void *FastAlloc (int size) = 0;
};


class QHUNK : public QALLOCATORBASE
{
public:
	virtual void *Alloc (int size) = 0;
	virtual void *FastAlloc (int size) = 0;
	virtual int GetLowMark (void) = 0;
	virtual void FreeToLowMark (int mark) = 0;

protected:
	byte *base;
	int size;
	int lowmark;
	int highmark;
};


class QHEAP : public QALLOCATORBASE
{
public:
	QHEAP (void);
	~QHEAP (void);
	void *Alloc (int size);
	void *FastAlloc (int size);
	void Free (void *data);
	void Discard (void);

private:
	void CreateHeap (void);
	HANDLE hHeap;
};


class QPOOL : public QHUNK
{
public:
	QPOOL (int maxsize);
	~QPOOL (void);

	void *Alloc (int size);
	void *FastAlloc (int size);
	int GetLowMark (void);
	void FreeToLowMark (int mark);
};


class QSCRATCH : public QHUNK
{
public:
	QSCRATCH (int maxsize);
	~QSCRATCH (void);

	void *Alloc (int size);
	void *FastAlloc (int size);
	int GetLowMark (void);
	void FreeToLowMark (int mark);
	void AdvanceLowMark (int advance);

	// only allow one temp allocation at a time; the pool must be explicitly unlocked
	// before another allocation of any kind can be made from it.
	void *Lock (char *caller);
	void Unlock (void);

private:
	char LockedFunction[1024];
	BOOL Locked;
	QHEAP *ScratchHeap;
};


class QSCRATCHLOCKER
{
public:
	QSCRATCHLOCKER (void **data, char *caller);
	~QSCRATCHLOCKER (void);
};

// these are our pools for use by various allocations
extern QHEAP *zoneHeap;
extern QPOOL *mapPool;
extern QPOOL *gamePool;
extern QSCRATCH *qScratch;


